package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.Video;
import com.ruoyi.system.mapper.ScenicAreaMapper;
import com.ruoyi.system.domain.ScenicArea;
import com.ruoyi.system.service.IScenicAreaService;

/**
 * 景区信息Service业务层处理
 * 
 * @author duantao
 * @date 2025-07-02
 */
@Service
public class ScenicAreaServiceImpl implements IScenicAreaService 
{
    @Autowired
    private ScenicAreaMapper scenicAreaMapper;

    /**
     * 查询景区信息
     * 
     * @param id 景区信息主键
     * @return 景区信息
     */
    @Override
    public ScenicArea selectScenicAreaById(Long id)
    {
        return scenicAreaMapper.selectScenicAreaById(id);
    }

    /**
     * 查询景区信息列表
     * 
     * @param scenicArea 景区信息
     * @return 景区信息
     */
    @Override
    public List<ScenicArea> selectScenicAreaList(ScenicArea scenicArea)
    {
        return scenicAreaMapper.selectScenicAreaList(scenicArea);
    }

    /**
     * 新增景区信息
     * 
     * @param scenicArea 景区信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertScenicArea(ScenicArea scenicArea)
    {
        scenicArea.setCreateTime(DateUtils.getNowDate());
        int rows = scenicAreaMapper.insertScenicArea(scenicArea);
        insertVideo(scenicArea);
        return rows;
    }

    /**
     * 修改景区信息
     * 
     * @param scenicArea 景区信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateScenicArea(ScenicArea scenicArea)
    {
        scenicArea.setUpdateTime(DateUtils.getNowDate());
        scenicAreaMapper.deleteVideoByScenicAreaId(scenicArea.getId());
        insertVideo(scenicArea);
        return scenicAreaMapper.updateScenicArea(scenicArea);
    }

    /**
     * 批量删除景区信息
     * 
     * @param ids 需要删除的景区信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScenicAreaByIds(Long[] ids)
    {
        scenicAreaMapper.deleteVideoByScenicAreaIds(ids);
        return scenicAreaMapper.deleteScenicAreaByIds(ids);
    }

    /**
     * 删除景区信息信息
     * 
     * @param id 景区信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScenicAreaById(Long id)
    {
        scenicAreaMapper.deleteVideoByScenicAreaId(id);
        return scenicAreaMapper.deleteScenicAreaById(id);
    }

    /**
     * 新增视频信息信息
     * 
     * @param scenicArea 景区信息对象
     */
    public void insertVideo(ScenicArea scenicArea)
    {
        List<Video> videoList = scenicArea.getVideoList();
        Long id = scenicArea.getId();
        if (StringUtils.isNotNull(videoList))
        {
            List<Video> list = new ArrayList<Video>();
            for (Video video : videoList)
            {
                video.setScenicAreaId(id);
                list.add(video);
            }
            if (list.size() > 0)
            {
                scenicAreaMapper.batchVideo(list);
            }
        }
    }
}
